package cn.zebra.dev.shop.module.shop.service.impl;

import cn.zebra.dev.ibatis.plus.service.impl.ServiceImpl;
import cn.zebra.dev.ibatis.plus.utils.PageUtils;
import cn.zebra.dev.ibatis.plus.utils.Query;
import cn.zebra.dev.shop.module.shop.dao.ShopDao;
import cn.zebra.dev.shop.module.shop.entity.ShopEntity;
import cn.zebra.dev.shop.module.shop.service.ShopService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * @author runnable@sina.cn
 */
@Service
public class ShopServiceImpl extends ServiceImpl<ShopDao, ShopEntity> implements ShopService {

    @Override
    public ShopEntity addShop(ShopEntity entity) {
        Long userId = entity.getShopUserId();
        QueryWrapper<ShopEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("shop_user_id", userId);
        List<ShopEntity> list = list(wrapper);
        if (list != null && list.size() > 0) {
            throw new RuntimeException("一个商户只能有一个店铺");
        }
        if (!save(entity)) {
            throw new RuntimeException("添加商户失败!");
        }
        return entity;
    }

    @Override
    public PageUtils<ShopEntity> queryPage(Integer pageNo, Integer pageSize, String shopName) {
        QueryWrapper<ShopEntity> wrapper = new QueryWrapper<>();
        if (shopName != null) {
            wrapper.eq("shop_name", shopName);
        }
        IPage<ShopEntity> iPage = this.page(
                new Query<ShopEntity>(pageNo, pageSize).getPage(),
                wrapper
        );
        return new PageUtils<>(iPage);
    }

    @Override
    public ShopEntity findShopByAdminId(Long userId) {
        QueryWrapper<ShopEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("shop_user_id", userId);
        List<ShopEntity> list = list(wrapper);
        if (list != null && list.size() > 0) {
            return list.get(0);
        }
        return null;
    }

    /**
     * 根据主键id集合查找店铺对象集合
     *
     * @param shopIds 店铺id集合
     * @return 店铺对象集合
     */
    @Override
    public List<ShopEntity> findByIds(Collection<Long> shopIds) {
        if (shopIds == null || shopIds.size() == 0) {
            return new ArrayList<>(2);
        }
        QueryWrapper<ShopEntity> wrapper = new QueryWrapper<>();
        wrapper.in("id", shopIds);
        return this.list(wrapper);
    }

    /**
     * 根据主键id查找店铺对象
     *
     * @param shopId 店铺id
     * @return 店铺对象
     */
    @Override
    public ShopEntity findById(Long shopId) {
        QueryWrapper<ShopEntity> wrapper = new QueryWrapper<>();
        wrapper.apply("id = {0}", shopId);
        return this.getOne(wrapper);
    }

    @Override
    public PageUtils<ShopEntity> findPage(Map<String, Object> params) {
        QueryWrapper<ShopEntity> wrapper = new QueryWrapper<>();
        IPage<ShopEntity> iPage = this.page(
                new Query<ShopEntity>(params).getPage(),
                wrapper
        );
        return new PageUtils<>(iPage);
    }

    @Override
    public List<ShopEntity> findByIds(List<Long> shopIds) {
        QueryWrapper<ShopEntity> wrapper = new QueryWrapper<>();
        wrapper.in("id", shopIds);
        return list(wrapper);
    }
}
